/**
 * jims
 */
package com.jims.master.modules.finance.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.finance.dao.InpAcctDetailDao;
import com.jims.master.modules.finance.dao.InpAcctMoneyDao;
import com.jims.master.modules.finance.dao.InpSettleMasterDao;
import com.jims.master.modules.finance.entity.InpAcctDetail;
import com.jims.master.modules.finance.entity.InpAcctMoney;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.utils.NumberToCN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.finance.entity.InpAcctMaster;
import com.jims.master.modules.finance.dao.InpAcctMasterDao;

/**
 * 结账主记录Service
 * @author 赵宁
 * @version 2017-03-30
 */
@Service
@Transactional(readOnly = true)
public class InpAcctMasterService extends CrudService<InpAcctMasterDao, InpAcctMaster> {
    @Autowired
	private InpAcctMoneyDao inpAcctMoneyDao;
	@Autowired
	private InpAcctDetailDao inpAcctDetailDao;
	@Autowired
	private InpSettleMasterDao inpSettleMasterDao;
	public InpAcctMaster get(String id) {
		return super.get(id);
	}
	
	public List<InpAcctMaster> findList(InpAcctMaster inpAcctMaster) {
		return super.findList(inpAcctMaster);
	}
	
	public Page<InpAcctMaster> findPage(Page<InpAcctMaster> page, InpAcctMaster inpAcctMaster) {
		return super.findPage(page, inpAcctMaster);
	}

	/**
	 * 出院结算结账确认
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	@Transactional(readOnly = false)
	public int save(InpAcctMaster inpAcctMaster) {
		int code=0;
         String acctNo = dao.getMaxAcctNo();
		if(StringUtils.isNotBlank(acctNo)&& acctNo!=null){
			int no= Integer.parseInt(acctNo)+1;
			acctNo = String.valueOf(no);
		}else{
			acctNo=""+28000;
		}
		InpAcctMaster acctMaster = dao.findRcptNo(inpAcctMaster);
		acctMaster.setAcctNo(acctNo);
		acctMaster.setChargesAmount(acctMaster.getSumAmount());
		acctMaster.setStartDate(inpAcctMaster.getStartDate());
		acctMaster.setEndDate(inpAcctMaster.getEndDate());
		acctMaster.setOrgId(inpAcctMaster.getOrgId());
		acctMaster.setAcctType("0");
		acctMaster.preInsert();
		code=dao.insert(acctMaster);
		List<InpAcctMoney> inpAcctMoneyList= dao.findRcptType(inpAcctMaster);
		if(inpAcctMoneyList!=null && inpAcctMoneyList.size()>0){
			 for(int i=0;i<inpAcctMoneyList.size();i++){
				 InpAcctMoney inpAcctMoney = inpAcctMoneyList.get(i);
				 inpAcctMoney.setAcctNo(acctNo);
				 inpAcctMoney.setAcctId(acctMaster.getId());
				 inpAcctMoney.setOrgId(acctMaster.getOrgId());
				 inpAcctMoney.preInsert();
				 inpAcctMoneyDao.insert(inpAcctMoney);
			 }
		}
		List<InpAcctDetail> inpAcctDetails = dao.findRcptDetail(inpAcctMaster);
		if(inpAcctDetails!=null && inpAcctDetails.size()>0){
			 for(int j=0;j<inpAcctDetails.size();j++){
                  InpAcctDetail inpAcctDetail=inpAcctDetails.get(j);
				  inpAcctDetail.setAcctNo(acctNo);
				  inpAcctDetail.setAcctId(acctMaster.getId());
				  inpAcctDetail.setOrgId(acctMaster.getOrgId());
				  inpAcctDetail.preInsert();
				  inpAcctDetailDao.insert(inpAcctDetail);
			 }
		}
        inpSettleMasterDao.updateSettle(inpAcctMaster);
		return code;
	}
	
	@Transactional(readOnly = false)
	public int delete(InpAcctMaster inpAcctMaster) {
		return super.delete(inpAcctMaster);
	}

	/**
	 * 查询结账主记录信息
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	public InpAcctMaster findRcptNo(InpAcctMaster inpAcctMaster){
	  return  dao.findRcptNo(inpAcctMaster);
	}

	/**
	 * 查询 结算类型
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	public List<InpAcctMoney> findRcptType(InpAcctMaster inpAcctMaster){
		 return dao.findRcptType(inpAcctMaster);
	}

	/**
	 * 查询结算详细项目
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	public List<InpAcctDetail> findRcptDetail(InpAcctMaster inpAcctMaster){
         return  dao.findRcptDetail(inpAcctMaster);
	}

	/**
	 * 住院结账打印
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	public String printInpAcct(InpAcctMaster inpAcctMaster){
		String printPath ="";
		User user =UserUtils.getUser();
		Map map = new HashMap();
		inpAcctMaster.setCreateBy(user);
		inpAcctMaster = dao.getAcctMaster(inpAcctMaster);
		BaseDto baseDto=inpSettleMasterDao.printInpAcct(inpAcctMaster);
		map.put("acctNo",inpAcctMaster.getAcctNo());
		map.put("acctDate", DateUtils.formatDateTime(inpAcctMaster.getCreateDate()));
		map.put("operator",inpAcctMaster.getCreateBy());
		map.put("totalCharges",inpAcctMaster.getChargesAmount());
		BigDecimal amonut = new BigDecimal(inpAcctMaster.getChargesAmount());
		String totalChargesD =NumberToCN.number2CNMontrayUnit(amonut);
		map.put("totalChargesD",totalChargesD);
		map.put("totalnum",baseDto.get("rcptsnum"));
		map.put("totalRefundNum",baseDto.get("refundnum"));
		map.put("refundCost",baseDto.get("refundamount"));
		map.put("invalidNum",baseDto.get("invalidnum"));
		map.put("invalidCost",baseDto.get("invalidamount"));
		map.put("minRcptNo",baseDto.get("minrcptno"));
		map.put("maxRcptNo",baseDto.get("maxrcptno"));
		map.put("rcptName",user.getName());
        List<BaseDto> list= inpAcctMoneyDao.printAcctSearchType(inpAcctMaster);
		printPath = PrintPdfUtils.previewPdf(user.getId(), "his/finance/inpChargesAcct.jasper", map, list, "inpChargesAcct");
		return printPath;
	}
}